<?php
# Base class for the RAWS interface classes.
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_once 'Zend/Gdata.php';
require_once 'Zend/Gdata/App.php';
require_once dirname(__FILE__) . '/Raws/Feed.php';
require_once dirname(__FILE__) . '/Raws/Entry.php';
require_once dirname(__FILE__) . '/Util.php';
require_once dirname(__FILE__) . '/Raws/Response.php';

class RawsHttpClient extends Zend_Http_Client
{
  public function setResponse($response) {
    $this->last_response = $response;
  }  
}

class Raws extends Zend_Gdata_App
{
  protected $_base_uri = null;
  protected $_path = null;
  protected $_user = null;
  protected $_pwd = null;

  /**
   * Service name for use with Google's authentication mechanisms
   *
   * @var string
   */
  const AUTH_SERVICE_NAME = 'raws_api';

  /**
   * Default URI to which to POST.
   *
   * @var string
   */
  protected $_defaultPostUri = null;

  /**
   * Packages to search for classes when using magic __call method, in order.
   *
   * @var array
   */
  protected $_registeredPackages = array(
          'Raws',
          'Raws_Extension',
          'Rass',
          'Rass_Extension',
          'Rams',
          'Rams_Extension',
          'Rats',
          'Rats_Extension',
          'Zend_Gdata_App_Extension',
          'Zend_Gdata_App');

  /**
   * Namespaces used for GData data
   *
   * @var array
   */
  public static $namespaces = array(
    'raws' => 'http://rambla.be/raws/ns-metadata/1.0');

  /**
   * Create Raws object
   *
   * @param Zend_Http_Client $client
   * @param string $applicationId The identity of the app in the form of Company-AppName-Version
   */
  public function __construct($user = null, $pwd = null, $base_uri = null, $path = null, $client = null, $applicationId = 'MyCompany-MyApp-1.0')
  {
    $tmpclient = new RawsHttpClient();
    $tmpclient->setConfig(array(
        'maxredirects' => 0,
        'timeout'      => 300,
        'keepalive'    => true));
    parent::__construct($tmpclient, $applicationId);
    
    $this->setBaseUri($base_uri);
    $this->setPath($path);
    if ($user && $pwd) {
      $this->setCredentials($user, $pwd);
    }
  }
    
  public function setPath($value)
  {
     $this->_path = $value;
  }

  public function getPath()
  {
      return $this->_path;
  }

  public function getBaseUri()
  {
      return $this->_base_uri;
  }

  public function setBaseUri($value)
  {
      $this->_base_uri = $value;
      return $this;
  }
  
  public function getUser()
  {
      return $this->_user;
  }
  
  
  function setCredentials($user,$pwd)
  {
    $this->_user = $user;
    $this->_pwd = $pwd;
    $client = $this->getHttpClient();
    // $client = self::getHttpClient();
    $client->setAuth($this->_user, $this->_pwd, Zend_Http_Client::AUTH_BASIC);
    return $this;
  }
    
  function getResponse()
  {
    $client = $this->getHttpClient();
    if (!$client) {
      return null;
    }
    return $client->getLastResponse();
  }

  function getRawsResponse()
  {
    return new RawsResponse($this->getResponse());
  }
  
  public static function parseRawsResponse($response)
  {
    if (!$response) {
      return null;
    }
    return new RawsResponse($response);
  }

  function unsetSlugHeader()
  {
    # make sure the slug header is removed again (this should be in the Zend base class, let's hope next release -> see issue http://framework.zend.com/issues/browse/ZF-3515)
    $this->getHttpClient()->setHeaders('slug', null);
  }  
}
